package com.company.dcm.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.company.dcm.common.R;
import com.company.dcm.constant.RoleConstant;
import com.company.dcm.entity.Dev;
import com.company.dcm.entity.User;
import com.company.dcm.entity.WorkShop;
import com.company.dcm.service.DevService;
import com.company.dcm.service.PermissionService;
import com.company.dcm.service.UserService;
import com.company.dcm.service.WorkShopService;
import com.company.dcm.utils.IdUtils;
import com.company.dcm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/workShop")
public class WorkShopController {
    @Autowired
    private WorkShopService workShopService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService userService;
    @Autowired
    private DevService devService;

    @SaCheckRole(RoleConstant.FACTORY_MANAGER)
    @PostMapping("/add")
    public R add(@RequestBody WorkShop workShop){
        if(StringUtils.isNullOrEmpty(workShop.getName()))return R.error("车间名不可为空！");

        User user = userService.getExistByUserName((String) StpUtil.getLoginId());
        List<Long> factoryIds= permissionService.getFactoryIdsByUserId(user.getUserId());
        if(factoryIds.size()==0)return R.error("请先获得分配的厂房！");

        Long factoryId = factoryIds.get(0);
        if(workShopService
                .count(new LambdaQueryWrapper<WorkShop>()
                .eq(WorkShop::getName,workShop.getName())
                .eq(WorkShop::getFactoryId,factoryId))>0){
            return R.error("车间名不可重复！");
        }

        workShop.setId(IdUtils.getInstance().NextId());
        workShop.setFactoryId(factoryId);
        if(workShopService.save(workShop)){
            return R.success("新增成功！");
        }
        return R.error("未知错误！");
    }

    @GetMapping("/list")
    public R list(Long factoryId){
        List<WorkShop> workShops=new ArrayList<>();
        if(factoryId==null){
            User user = userService.getExistByUserName((String) StpUtil.getLoginId());
            List<Long> workShopIds= permissionService.getWorkShopIdsByUserId(user.getUserId());
            for (Long workShopId:workShopIds){
                workShops.add(workShopService.getById(workShopId));
            }
        }else {
            List<WorkShop> workShopList = workShopService.list(new LambdaQueryWrapper<WorkShop>().eq(WorkShop::getFactoryId,factoryId));
            for (WorkShop workShop:workShopList){
                workShops.add(workShopService.getById(workShop.getId()));
            }
        }

        return R.success(workShops);
    }

    @PostMapping("/delete")
    public R delete(@RequestBody WorkShop workShop){
        if(workShop.getId()==null)return R.error("车间不可为空！");

        User user = userService.getExistByUserName((String) StpUtil.getLoginId());
        List<Long> factoryIds= permissionService.getFactoryIdsByUserId(user.getUserId());
        if(factoryIds.size()==0)return R.error("请先获得分配的厂房！");

        Long factoryId = factoryIds.get(0);
        workShop = workShopService.getOne(new LambdaQueryWrapper<WorkShop>()
                        .eq(WorkShop::getId,workShop.getId())
                        .eq(WorkShop::getFactoryId,factoryId));

        if(workShop==null){
            return R.error("该车间不存在！");
        }

        if(devService.count(new LambdaQueryWrapper<Dev>().eq(Dev::getId,workShop.getId()))>0){
            return R.error("请先处理该车间下的机台！");
        }

        workShopService.remove(new LambdaQueryWrapper<WorkShop>().eq(WorkShop::getId,workShop.getId()));
        return R.success("删除成功！");
    }
}
